package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.QuestionBank;
import com.battle.pojo.entity.QuestionBankType;
import com.battle.pojo.entity.User;
import com.battle.pojo.param.questionBankType.*;
import com.battle.pojo.vo.questionBankType.*;
import com.battle.pojo.dto.questionBankType.*;
import com.battle.service.repository.in.QuestionBankRepository;
import com.battle.service.repository.in.QuestionBankTypeRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.service.in.QuestionBankTypeService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.exceptions.BizException;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 【描 述】：题库分类服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class QuestionBankTypeServiceImpl implements QuestionBankTypeService {

    private final QuestionBankTypeRepository questionBankTypeRepository;
    private final QuestionBankRepository questionBankRepository;
    private final UserRepository userRepository;
    public QuestionBankTypeServiceImpl(QuestionBankTypeRepository questionBankTypeRepository, QuestionBankRepository questionBankRepository, UserRepository userRepository) {
        this.questionBankTypeRepository = questionBankTypeRepository;
        this.questionBankRepository = questionBankRepository;
        this.userRepository = userRepository;
    }


    /**
     * 创建题库分类
     *
     * @param param
     */
    @Override
    public void createQuestionBankType(CreateQuestionBankTypeParam param) {
        QuestionBankType questionBankType = new QuestionBankType();
        BeanUtils.copyProperties(param, questionBankType);
        Long id = IdGenerator.SNOW.generate();
        questionBankType.setId(id);
        questionBankTypeRepository.create(questionBankType);
    }

    /**
     * 修改题库分类
     *
     * @param param
     */
    @Override
    public void editQuestionBankType(EditQuestionBankTypeParam param) {
        Long id = param.getId();
        QuestionBankType questionBankType = questionBankTypeRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, questionBankType);
        questionBankTypeRepository.edit(questionBankType);
    }

    /**
     * 移除题库分类
     *
     * @param id
     */
    @Override
    public void removeQuestionBankType(Long id) {
        //检查下面是否有题目
        List<QuestionBank> questionBanks = questionBankRepository.getBaseMapper().selectList(new LambdaQueryWrapper<QuestionBank>()
                .eq(QuestionBank::getTypeId, id)
                .eq(QuestionBank::getIsDelete, 0));
        if (CollectionUtil.isNotEmpty(questionBanks)) {
            throw new BizException("该题库分类下有题目，不能删除");
        }
        questionBankTypeRepository.removeById(id);
    }

    /**
     * 启动/禁用题库分类
     *
     * @param id
     */
    @Override
    public void enabledQuestionBankType(Long id) {
        QuestionBankType questionBankType = questionBankTypeRepository.getByIdCheck(id);
        questionBankType.setIsDisable(!questionBankType.getIsDisable());
        questionBankTypeRepository.edit(questionBankType);
    }

    /**
     * 获取题库分类详情
     *
     * @param id
     * @return
     */
    @Override
    public GetQuestionBankTypeVo getQuestionBankType(Long id) {
        QuestionBankType questionBankType = questionBankTypeRepository.getByIdCheck(id);
        GetQuestionBankTypeVo vo = new GetQuestionBankTypeVo();
        BeanUtils.copyProperties(questionBankType, vo);
        return vo;
    }

    /**
     * 获取题库分类 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetQuestionBankTypeListVo> getQuestionBankTypeList(GetQuestionBankTypeListParam param) {

        SelectQuestionBankTypeDto dto = new SelectQuestionBankTypeDto();
        BeanUtils.copyProperties(param, dto);
        Page<QuestionBankType> page = questionBankTypeRepository.selectQuestionBankType(dto);
        List<QuestionBankType> questionBankTypeList = page.getRecords();
        List<GetQuestionBankTypeListVo> list = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(questionBankTypeList)){
            List<Long> userIds = new ArrayList<>();
            questionBankTypeList.forEach(v -> {
                userIds.add(v.getCreator());
            });
            Map<Long, User> userMap = userRepository.getMap(userIds);
            for (QuestionBankType questionBankType : questionBankTypeList) {
                GetQuestionBankTypeListVo vo = new GetQuestionBankTypeListVo();
                BeanUtils.copyProperties(questionBankType, vo);
                User user = userMap.get(questionBankType.getCreator());
                if (user!=null){
                    vo.setCreateName(user.getName());
                }
                //获取题库数量
                Long questionBankCount = questionBankRepository.getBaseMapper().selectCount(new LambdaQueryWrapper<QuestionBank>()
                        .eq(QuestionBank::getTypeId, questionBankType.getId())
                        .eq(QuestionBank::getIsDelete, 0));
                vo.setQuestionBankCount(questionBankCount.intValue());
                list.add(vo);
            }
        }

        return new PageResult<>(list, page);

    }

    /**
     * 获取题库分类下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetQuestionBankTypeDownListVo> getQuestionBankTypeDownList(GetQuestionBankTypeDownListParam param) {
        QuestionBankType po = new QuestionBankType();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<QuestionBankType> questionBankTypeList = questionBankTypeRepository.getListByEntity(po);

        List<GetQuestionBankTypeDownListVo> list = new ArrayList<>();
        for (QuestionBankType questionBankType : questionBankTypeList) {
            GetQuestionBankTypeDownListVo vo = new GetQuestionBankTypeDownListVo();
            BeanUtils.copyProperties(questionBankType, vo);
            list.add(vo);
        }
        return list;
    }


}



